using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;
using System.Configuration;

namespace DAL
{
    public class tbDanhMucSanPhamDAL
    {
        private SqlConnection cn = null;
        public tbDanhMucSanPhamDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }

        public tbDanhMucSanPhamDAL(tbDanhMucSanPhamDAL tan)
        {
            this.cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            this.cn.Open();
        }

        public void Insert(tbDanhMucSanPham th)
        {
            SqlCommand cm = new SqlCommand("tbDanhMucSanPham_insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@tendanhmuc", th.tendanhmuc);       
            cm.ExecuteNonQuery();
        }
        public void Update(tbDanhMucSanPham th)
        {
            SqlCommand cm = new SqlCommand("tbDanhMucSanPham_update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@madanhmuc", th.madanhmuc);
            cm.Parameters.AddWithValue("@tendanhmuc", th.tendanhmuc);
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("tbDanhMucSanPham_delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@madanhmuc", iD);
            cm.ExecuteNonQuery();
        }
        public tbDanhMucSanPhamCollection Get(string where, int pageIndex, int pageSize,
            string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("tbDanhMucSanPham_GET", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;
            SqlDataReader dr = cm.ExecuteReader();
            tbDanhMucSanPhamCollection dmCollection = new tbDanhMucSanPhamCollection();
            while (dr.Read())
            {
                tbDanhMucSanPham th = new tbDanhMucSanPham();
                th.madanhmuc = int.Parse(dr.GetValue(0).ToString());
                th.tendanhmuc = dr.GetValue(1).ToString();
                dmCollection.Add(th);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return dmCollection;
        }
    }
}
